import os

"""
    将airfoil_dir下的所有翼型的坐标点方向原地反转
"""


def rev(file_path):
    """
        不带表头反转
    :param file_path: 文件路径
    :return:
    """
    with open(file_path, 'r') as f:
        lines = f.readlines()[::-1]
        lines[-1] = lines[-1].strip()

    with open(file_path, 'w') as f:
        f.writelines(lines)


def reverse_with_header(file_path):
    '''
        带表头翼型坐标文件反转
    :param file_path: 文件路径
    :return:
    '''
    lines = []
    with open(file_path, 'r') as f:
        tmp_lines = f.readlines()
        lines.append(tmp_lines[0])
        lines += tmp_lines[1::][::-1]

    with open(file_path, 'w') as f:
        f.writelines(lines)


def loop_in_dir(airfoil_dir):
    """
        将文件夹中的所有翼型坐标文件进行反转
    :param airfoil_dir: 翼型文件夹
    :return:
    """
    for file in os.listdir(airfoil_dir):
        file_path = os.path.join(airfoil_dir, file)
        reverse_with_header(file_path)


if __name__ == "__main__":

    loop_in_dir(r"/home/linjx/data/dataset/UIUC_AND_NACA")
